package com.hivision.streetlight.codec;

import java.io.UnsupportedEncodingException;

public class Utilty {

    private static Utilty instance = new Utilty();

    public static Utilty getInstance() {
        return instance;
    }

    public static final int MIN_MID_VALUE = 1;

    public static final int MAX_MID_VALUE = 65535;

    /**
     * 大端
     * @param b
     * @param start
     * @param length
     * @return
     */
    public int bytes2IntInBig(byte[] b, int start, int length) {
        int sum = 0;
        int end = start + length;
        for (int k = start; k < end; k++) {
            int n = ((int) b[k]) & 0xff;
            n <<= (--length) * 8;
            sum = sum | n;
        }
        return sum;
    }
    
    /**
     * 大端模式
     * @param value
     * @param length
     * @return
     */
    public byte[] int2BytesInBig(int value, int length) {
        byte[] b = new byte[length];
        for (int k = 0; k < length; k++) {
            b[length - k - 1] = (byte) ((value >> 8 * k) & 0xff);
        }
        return b;
    }
    
    
    /**
     * 小端
     * @param b
     * @param start
     * @param length
     * @return
     */
    public int bytes2Int(byte[] b, int start, int length) {
        int sum = 0;
        int end = start + length;
//        for (int k = start; k < end; k++) {
//            int n = ((int) b[k]) & 0xff;
//            n <<= (--length) * 8;
//            sum += n;
//        }
        for (int k = end-1; k >= start; k--) {
            int n = ((int) b[k]) & 0xff;
            n <<= (--length) * 8;
            sum += n;
        }
        return sum;
    }

    /**
     * 小端模式
     * @param value
     * @param length
     * @return
     */
    public byte[] int2Bytes(int value, int length) {
        byte[] b = new byte[length];
        for (int k = 0; k < length; k++) {
            b[k] = (byte) ((value >> 8 * k) & 0xff);
        }
        return b;
    }
    

    public boolean isValidofMid(int mId) {
        if (mId < MIN_MID_VALUE || mId > MAX_MID_VALUE) {
            return false;
        }
        return true;
    }

    public static String parseByte2HexStr(byte[] buf) {
        if (null == buf || buf.length==0) {
            return null;
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }
    public static byte[] hexString2Bytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            
        }
        return d;
    }
    
    public String parseByte2AscStr(byte[] buf, int start, int length) throws UnsupportedEncodingException{
        if (null == buf) {
            return null;
        }
        
        byte[] sim = new byte[length];
        for(int i=0;i<length;i++){
        	sim[i] = buf[start+i];
        }
        
        return new String(sim, "ISO8859-1");
//	    StringBuilder sb = new StringBuilder(length);
//	    for (int i = start; i < start+length; ++ i) {
//	        if (buf[i] < 0) throw new IllegalArgumentException();
//	        sb.append((char) buf[i]);
//	    }
//	    return sb.toString();
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
    
    public String parseByte2IpStr(byte[] buf, int start){
        if (null == buf) {
            return null;
        }
	    StringBuilder sb = new StringBuilder(4);
	    for (int i = start; i < start+4; ++ i) {
	        if (buf[i] < 0) throw new IllegalArgumentException();
	        sb.append(".").append(Integer.valueOf(buf[i]));
	    }
	    return sb.toString().substring(1);
    }
    
    public static void main(String[] args){
    	byte[] b = {1,44,45};
    	int c = Utilty.getInstance().bytes2Int(b,0,2);
    	System.out.println(String.valueOf(c));
	}
}
